package org.aceor.mddal.gateway.datatype.impl;

import com.google.common.math.LongMath;
import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.datatype.cal.AbstractCalculator;
import org.aceor.mddal.gateway.datatype.cal.Calculator;

import java.math.RoundingMode;
import java.sql.Timestamp;

/**
 * Created by lxue on 16/4/16.
 */
public class TimestampType implements DataType<Timestamp> {
    private Calculator calculator = new AbstractCalculator() {
        @Override
        public Object doAdd(Object value1, Object value2) {
            return LongMath.checkedAdd(((Timestamp)value1).getTime(), ((Timestamp)value2).getTime());
        }

        @Override
        public Object doSub(Object value1, Object value2) {
            return LongMath.checkedSubtract(((Timestamp)value1).getTime(), ((Timestamp)value2).getTime());
        }

        @Override
        public Object doMultiply(Object value1, Object value2) {
            return LongMath.checkedMultiply(((Timestamp)value1).getTime(), ((Timestamp)value2).getTime());
        }

        @Override
        public Object doDivide(Object value1, Object value2) {
            return LongMath.divide(((Timestamp)value1).getTime(), ((Timestamp)value2).getTime(), RoundingMode.DOWN);
        }
    };
    @Override
    public int compare(Timestamp o1, Timestamp o2) {
        long result = o1.getTime() - o2.getTime();
        if (result < 0) {
            return -1;
        } else if (result == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public Calculator getCalculator() {
        return calculator;
    }
}
